<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/back/css/base.css" rel="stylesheet" type="text/css" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
</head>
<body>
	<div class="panel panel-info panel-content">
		<div class="panel-heading" style="position: fixed;height: 120px;width: 99%;">
		<table>
			<tr>
				<th >项目名称:</th>
				<td>${enquiry.title} </td>
				<td class="w80 "></td>
				<th>发布公司:</th>
				<td>${enquiry.customer.company} </td>
				<td class="w80 "></td>
				<th>所在地:</th>
				<td> 
					[#list enquiry.providerarea?split(",") as province ]
					[#if provinces?? ]
						${provinces[province!]} &nbsp;
					[#else]
						${province} &nbsp;
					[/#if]
						
					[/#list]
				</td>
				<td class="w80 "></td>
				<th>截止日期:</th>
				<td>[#if enquiry.deadline?? ]${enquiry.deadline?string("yyyy-MM-dd")!''}[#else]无[/#if]</td>
			</tr>
			<tr>
				<th>联系人</th>
				<td>${enquiry.contacts!}</td>
				<td class="w80 "></td>
				<th>联系电话:</th>
				<td>${enquiry.phone!}</td>
			</tr>
			<tr>
				<th>
					当前状态：
				</th>
				<td>
					${enquiry.prostateName!}
				</td>
				<td class="w80 "></td>
				<th>操作:</th>
				<td>
				[#if enquiry.prostate == 0 || enquiry.prostate == 3  ]
					<button type="button" class="btn   btn-success" onclick="changeState(${enquiry.id},1)" >通过审核</button>
					<button type="button" class="btn   btn-default" onclick="changeState(${enquiry.id},2)" >退回修改</button>
				[#elseif enquiry.prostate == 1  ]	
					<button type="button" class="btn    btn-default" onclick="changeState(${enquiry.id},2)" >退回修改</button>
				[/#if ]	
				</td>
			</tr>
			<tr>
				<td>
					<button type="button" onclick="additem()" class="btn btn-info" >增加</button>
				</td>
				<td>
					<button type="button" onclick="submodel()" class="btn btn-info" >保存修改</button>
				</td>
			</tr>
		</table>
		
		
		</div>
	
	<form id="itemform"  action="/purchase/o_saveitem2.do" method="post"  style="padding-top: 120px;"  >
	<div >
		<input type="hidden" value="${enquiry.id}" name="enquiryid"  id="enquiryid"/>
		<input type="hidden" value="true" name="onlysave" />
		
		<table  id="fenxi"  class="table table-hover table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th>#</th>
					<th>系统名称</th>
					[#list 1..6  as a]
					<th class="cell_${a}">
						<select class="typename" name="typename" attnum=${a} >
							[#--标题匹配--]
							<option  value="0"  >请选择</option>
							<option  [#if a==1] selected="selected" [/#if] value="1"  >设备名称</option>
							<option  [#if a==2] selected="selected" [/#if] value="2" >品牌</option>
							<option  [#if a==3] selected="selected" [/#if] value="3" >型号</option>
							<option  [#if a==4] selected="selected" [/#if] value="4" >单位</option>
							<option  [#if a==5] selected="selected" [/#if] value="5" >数量</option>
							<option  [#if a==6] selected="selected" [/#if] value="6" >技术参数</option>
						</select>
						
					</th>
					[/#list]
					<th>
						操作
					</th>
				</tr>
			</thead>
			[#list pagination.list as enquiryitem ]
			<tr attindex="${enquiryitem_index}" >
				<td><span>${pagination.pageSize * (pagination.pageNo-1) + enquiryitem_index+1!}</span>
					<span>
						<input type="hidden"  name="item[${enquiryitem_index}].seriesnumber" value="${enquiry.seriesnumber!}"  />
						<input type="hidden"  name="item[${enquiryitem_index}].enquiry.id" value="${enquiry.id!}"  />
						<input type="hidden"  class="fcatename"  name="item[${enquiryitem_index}].fcatename" value="${enquiry.fcatename!}"  />
						<input type="hidden"  name="item[${enquiryitem_index}].processid" value="${enquiry.processes[0].id!}"  />
					</span>
				</td>
				<td class="cell_0">
					<select class="system_item" name="item[${enquiryitem_index}].fcateid"  >
					[#list pcats as cat]
						<option  value=${cat.id}  [#if  cat.id == enquiryitem.fcateid  ]selected="selected" [/#if]  >${cat.name}</option>							
					[/#list]
					</select>
				</td>
				<td class="cell_1" style="max-width: 150px;" >
				[#--<div style="max-height: 50px;overflow:auto;"> --]
				<input   name="item[${enquiryitem_index}]"    class="input_1"     value="${enquiryitem.productname!}" >
				</td>
				<td class="cell_2"><input name="item[${enquiryitem_index}]"    class="input_2" value="${enquiryitem.industry!}"></td>
				<td class="cell_3"><input name="item[${enquiryitem_index}]"    class="input_3" value="${enquiryitem.model!}"></td>
				<td class="cell_4" ><input name="item[${enquiryitem_index}]"    class="input_4" value="${enquiryitem.unit!}"></td>
				<td class="cell_5" ><input name="item[${enquiryitem_index}]"    class="input_5" value="${enquiryitem.count!}"></td>
				<td class="cell_6" style="max-width: 150px;"><input name="item[${enquiryitem_index}]"    class="input_6" value="${enquiryitem.descr!}"></td>
				<td>
					<button type="button" class="btn btn-xs btn-warning"  onclick="deletetr(this)" >删除</button>
				</td>
			</tr>
			[/#list]
		</table>
		</div>
		</form>
		<div>
			[#if aztQQs??]
				[#list aztQQs as aq]
					<label style="font-weight: normal;">
						<input type="radio" name="aq" value="${aq.qq}" [#if enquiry.aztQq?? && enquiry.aztQq == aq.qq]checked[/#if] >
						${aq.username!}&nbsp;&nbsp;${aq.qq!}&nbsp;&nbsp;进行中的询价单：${aq.count!}个
					</label>
				[/#list]
			[/#if]
		</div>
	</div>
	<div style="display: none;">
		<tr>
			<td>
				
			</td>
		</tr>
		
	</div>
<style>
.aui_content{width:100%;height: 100%;padding: 0px;}	
.noselect{
	background-color: rgba(99, 230, 99, 0.8);
}
</style>

<script>

	$("input[name='aq']").on('click' , function(){
		var aztqq = $(this).val();
		var enquiryid = $('#enquiryid').val();
		if(aztqq && enquiryid){
			$.ajax({
				url : '/purchase/o_set_aztqq.do',
				type : 'post',
				data : {enquiryid : enquiryid , aztqq:aztqq}
			});
		}
	});


	$(".aui_content").removeAttr("style");
	//切换状态
	function changeState(enquiryid ,prostate){
		art.dialog.load("请做出选择","/purchase/v_prostate_change.do",{enquiryid:enquiryid,prostate:prostate});
	}
	
	//切换状态后回调
	function changestatrecallback(){
		top.closeTempTab(true);
	}
	
	//添加行
	function additem(){
		var ctr = $("#fenxi").find("tr").last().clone(true);
		var indexnum = parseInt(ctr.attr("attindex"))+1;
		ctr.attr("attindex",indexnum);
		
		
		//序号	
		ctr.find("td").eq(0).find("span").eq(0).text(indexnum+1);
		var ctrhtml = ctr.html();
		
		var abc = ctrhtml.replace(/item\[(\d+)\]/gm,"item["+indexnum+"]");
		//重置tr里的内容
		ctr.html(abc);
		
		ctr.find("input[class^='input_']").val("");
		
		$("#fenxi").append(ctr);
		
		$(".panel-content").scrollTop($(".panel-content")[0].scrollHeight);
		
	}
	//删除行
	function deletetr(obj){
		art.dialog.confirm("确认删除",function(){
		
		var _this = $(obj);
		var _parent = _this.parents("tr");
		var indexnum = parseInt(_parent.attr("attindex"));
		_parent.nextAll().each(function(){
			$(this).attr("attindex",indexnum);
			$(this).find("td").eq(0).find("span").eq(0).text(indexnum+1);
			$(this).html($(this).html().replace(/item\[(\d+)\]/gm,"item["+indexnum+"]"));
			indexnum++;
		})
		_parent.remove();
		art.dialog.tips("已删除");	
		});
	}
	
	
	//设备名称，品牌 ，型号  ,单位 ,数量,技术参数
	var selectnames =["productname","industry","model","unit","count","descr"];
    //切换选项，确保不重复就可
	$(".typename").on("change",function(){
		//获取当前选值
		var  thisval = $(this).val();
		
		//将其他相同的选项设置为请选择
		if(thisval!=0){
			var hadvnum =[];
			hadvnum.push(thisval);
			
			$(".typename").each(function(i){
				var otherv = $(this).val();
				if(otherv == thisval ){
					$(this).val(0);
				}else{
					hadvnum.push(otherv);
				}
			});
			
			//没有被选择的加颜色
			var nums = hadvnum.join("");
			$(".noselect").removeClass("noselect");
			for(var i=1;i<7;i++){
				if(nums.indexOf(i)==-1){
					$(".typename").find("option[value='"+i+"']").addClass("noselect");
				}
			} 
			
		}
		
		$(this).val(thisval);
	})
	
	
			
	//切换系统
	$(".system_item").on("change",function(){
		var vv = $(this).val();
		$(".system_item").each(function(){
			$(this).val(vv);
		})
		var vname = $(this).find("option:selected").text();
		$(".fcatename").val(vname);
	});
    
    
    
	//准备提交
	function submodel(){
		var success = true;
		var msg ="";
		$(".typename").each(function(){
			var thisval = $(this).val();
			if(thisval==0){
				success = false;
				msg="失败：请确定每一列的名称";
				return ;
			}
			
		})
		
		if(!success){
			art.dialog.tips(msg);
			return ;
		}else{
			//确定每列的name
			$(".typename").each(function(i){
				var thisval = $(this).val();
				var attnum = $(this).attr("attnum");
				//拿到name值
				var namev = selectnames[thisval-1];
				//设置name值
				$(".input_"+attnum).each(function(){
					var oname = $(this).attr("name");
					var newname ="";
					if(oname.indexOf(".")!=-1){
						newname = oname.split(".")[0] +"."+namev;
					}else{
						newname = oname+"."+namev;
					}
					$(this).attr("name",newname);
				})
				
			})
			
			//验证数值 ,数量必须全为数字
			$("#itemform").find("input[name$='count']").each(function(){
				var num = $(this).val();
				if(isNaN(num)){
					success = false;
					return ;
				}
			})
			
			if(!success){
				art.dialog.tips("数量对应的列必须全为数字");
				return ;
			}
			
			//验证设备名称
			$("#itemform").find("input[name$='productname']").each(function(){
				var productname = $(this).val();
				if(productname==""){
					success = false;
					return ;
				}
			})
			
			if(!success){
				art.dialog.tips("设备名称不能有空！");
				return ;
			}
			
			//单位不能为空
			$("#itemform").find("input[name$='unit']").each(function(){
				var unit = $(this).val();
				if(unit==""){
					success = false;
					return ;
				}
			})
			
			if(!success){
				art.dialog.tips("单位不能有空！");
				return ;
			}
			
			
			
				//设置后提交
				art.dialog.post({
				url:"/purchase/o_saveitem.do",
				data:$("#itemform").serialize(),
				success:function(data){
					if(data.success){
						art.dialog.alert("保存成功",function(){
							top.closeTempTab(true);
						})
					}else{
						art.dialog.tips(data.msg);
					}
				}
			});   
			
		}
	}
</script>
</body>
</html>	